#include <iostream>
#include <cstdlib>
#include <cstring>
#include "memory.h"
using std::cerr;


//------------- CREATE NEW MEMORY CELL ----------------------------------------
struct Toc *alloc_new()
{
	struct Toc *new_item = NULL;

	new_item	= new 	(std::nothrow) struct Toc;

	if(new_item == NULL)
	{
		cerr << "Memory allocate error\n";
		exit(-1);
	}

	memset(new_item->_num,'0',6);
	//memset(new_item->_ress,0,7);
	new_item->_ress[0] = 0;
	new_item->_ress[1] = 0;
	new_item->_ress[2] = 0;
	new_item->_ress[3] = 0;

	new_item->_next = NULL;
	new_item->_ball = 0;
	return(new_item);
}
//------------------ CLEARE MEMORY ALLOCATED ----------------------------------
void clear_memory(struct Toc *&head)
{
	struct Toc *temp = NULL;
		
	while(head != NULL)
	{
		temp = head;
		head = head->_next;
		delete temp;
	}

}

int counted(struct Toc *head)
{
	const struct Toc *temp;
	int counter = 0;
	temp = head;
	
	while(head != NULL)
	{
		counter++;
		head = head->_next;
	}

	return(counter);
}


void rebuild_memory_by_param(struct Toc *&head)
{
	;
}

struct Toc *find_next(struct Toc *head)
{
	struct Toc *temp;
	head = head->_next;
	
	while(head != NULL )
	{
		
		if(temp->_ress[4] > 2 || temp->_ress[5] > 0 || temp->_ress[6] > 0)
		{
			return(head);
		}	
		temp = head;
		head = head->_next;
		delete temp;
	}
	
	return(NULL);
}
